/// @file Workspace.h
#pragma once
#include <gui/TanContainer.h>
#include <core/ContainerWidget.h>
#include <core/TanPainter.h>
#include <core/utility.h>
#include <QMouseEvent>
#include <QPoint>
#include <QPointF>

namespace qtangram
{
/// @class Workspace
/// @brief Main game area
class Workspace : public ContainerWidget, public TanContainer
{
    Q_OBJECT
public:
    Workspace(double viewRange, QWidget* parent = 0);
    Workspace(QWidget* parent = 0);
    void paintEvent(QPaintEvent *);
    /// Returns the pattern polygon
    const QMultiMap<unsigned int, Tan>& getPatternPolygon() const;
    /// Clears the pattern
    void clearPattern();
    QPolygonF sum;
public slots:
    void setPatternPolygon(QLinkedList<Tan> polygon);
#ifdef __DEBUG__
    void onToleranceChange(double tol);
#endif
protected:
    void mousePressEvent(QMouseEvent*);
    void mouseMoveEvent(QMouseEvent*);
    void mouseReleaseEvent(QMouseEvent*);
    /// True in drag mode
    bool dragStatus;
    QPoint lastMousePos;
    QPoint dragTranslation;
    /// Recalculates the selection area
    void calculateSelectionArea();
    /// True in selection mode
    bool selStatus;
    QPoint selStart;
    QPoint selEnd;
    /// True in rotation mode
    bool rotStatus;
    double lastAngle;
    QPointF rotCenter;
    /// Tries to move selected Tans to given location
    bool moveSelectedTo(QPoint pos);
    QMultiMap<unsigned int, Tan> patternPolygon;
    QPolygonF pattern;
private:
    #ifdef __DEBUG__
    double tolerance;
    QLinkedList<Tan> poly; //not necessary only for test
    #endif
signals:
    void sigRemoveSelectedTans();
    void sigCheckIfWon();
};
inline const QMultiMap<unsigned int, Tan> & Workspace::getPatternPolygon() const {
    return patternPolygon;
}
}
